package com.spicesoft.clientobjects.core;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanMetadata {

  private Map<Method, PropertyMetadata> getterMethodIndex = new HashMap<Method, PropertyMetadata>();
  private Map<Method, PropertyMetadata> setterMethodIndex = new HashMap<Method, PropertyMetadata>();
  private Map<Method, PropertyMetadata> indexedGetterMethodIndex = new HashMap<Method, PropertyMetadata>();
  private Map<Method, PropertyMetadata> indexedSetterMethodIndex = new HashMap<Method, PropertyMetadata>();
  private List<PropertyMetadata> enhancedPropertyIndex = new ArrayList<PropertyMetadata>();

  public BeanMetadata(List<PropertyMetadata> propertyList) {
    if (propertyList == null) {
      throw new IllegalArgumentException("null argument passed for propertyList");
    }
    
    for (PropertyMetadata pm : propertyList) {
      this.getterMethodIndex.put(pm.getGetterMethod(), pm);
      this.setterMethodIndex.put(pm.getSetterMethod(), pm);
      this.indexedGetterMethodIndex.put(pm.getIndexedGetterMethod(), pm);
      this.indexedSetterMethodIndex.put(pm.getIndexedSetterMethod(), pm);
      
      if (pm.isEnhanced()) {
        this.enhancedPropertyIndex.add(pm);
      }
    }
  }

  public PropertyMetadata getPropertyBySetter(Method method) {
    return this.setterMethodIndex.get(method);
  }

  public List<PropertyMetadata> getEnhancedProperties() {
    return this.enhancedPropertyIndex;
  }

  public PropertyMetadata getPropertyByGetter(Method method) {
    return this.getterMethodIndex.get(method);
  }

  public PropertyMetadata getPropertyByIndexedSetter(Method method) {
    return this.indexedSetterMethodIndex.get(method);
  }

  public PropertyMetadata getPropertyByIndexedGetter(Method method) {
    return this.indexedGetterMethodIndex.get(method);
  }
  
}
